/*
day39
2019.10.6
*/

#include<stdio.h>
#include<stdlib.h>
struct ListNode {
    int val;
    struct ListNode *next;
};
 
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
	struct ListNode *p1=l1,*p2=l2;
	struct ListNode *head=(struct ListNode *)malloc(sizeof(struct ListNode));
	struct ListNode *p3=head;
	int sum=0,cur=0;
	while(p1&&p2)
	{
		int num1=p1->val;
		int num2=p2->val;
		sum=num1+num2+cur;
		
		cur=sum/10;
		struct ListNode *p=(struct ListNode *)malloc(sizeof(struct ListNode));
		p->val=sum%10;
		p->next=NULL;
		p3->next=p;
		p1=p1->next;
		p2=p2->next; 
		p3=p3->next ;
	}
	while(p1)
	{
		int num1=p1->val;
		sum=num1+cur;
		cur=sum/10;
		struct ListNode *p=(struct ListNode *)malloc(sizeof(struct ListNode));
		p->val=sum%10;
		p->next=NULL;
		p3->next=p;
		p1=p1->next;
		p3=p3->next ;
	}
	
	while(p2)
	{
		int num2=p2->val;
		sum=num2+cur;
		cur=sum/10;
		struct ListNode *p=(struct ListNode *)malloc(sizeof(struct ListNode));
		p->val=sum%10;
		p->next=NULL;
		p3->next=p;
		p2=p2->next;
		p3=p3->next ;
	}
	
	if(cur)
	{
		struct ListNode *p=(struct ListNode *)malloc(sizeof(struct ListNode));
		p->val=cur;
		p->next=NULL;
		p3->next=p;
	}
	return head->next;
	
	
}


int main()
{
	return 0; 
}
